package com.kinon.ceshiaccount.sunmi;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.ceshiaccount.constant.Constant;
import com.kinon.ceshiaccount.sunmi.bean.HeadBean;
import com.kinon.ceshiaccount.sunmi.bean.ItemBean;
import com.kinon.ceshiaccount.sunmi.bean.KVP;
import com.kinon.ceshiaccount.sunmi.bean.ListingBean;
import com.kinon.ceshiaccount.sunmi.constatus.SunmiImgPath;
import com.kinon.ceshiaccount.sunmi.data.DataModel;
import com.kinon.ceshiaccount.sunmi.data.UPacketFactory;
import com.kinon.ceshiaccount.sunmi.util.SharedPreferencesUtil;
import com.kinon.ceshiaccount.util.SPManage;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import sunmi.ds.DSKernel;
import sunmi.ds.callback.ICheckFileCallback;
import sunmi.ds.callback.IConnectionCallback;
import sunmi.ds.callback.ISendCallback;
import sunmi.ds.data.DSData;
import sunmi.ds.data.DataPacket;

/**
 * Created by luohao on 2017-08-02.
 */

public class SunmiSdkManage {
    private Context mContext;
    public static  SunmiSdkManage summiManage=null;
    public  static DSKernel mDSKernel=null;
    private static HeadBean mHeadBean=null;


    private SunmiSdkManage(){
        mDSKernel =DSKernel.newInstance();
    }

    public static SunmiSdkManage getInstance(){
        if(summiManage==null || mDSKernel==null){
            summiManage=new SunmiSdkManage();
        }
        return summiManage;
    }

    public void sunmiInit(Context context,IConnectionCallback mConnCallback){
        mContext=context;
        mDSKernel.init(context, mConnCallback);
//        mDSKernel.addReceiveCallback(mReceiveCallback);
    }


    public void checkImg(Context context,long fileId, final String key){

        SunmiSdkManage.mDSKernel.checkFileExist(fileId, new ICheckFileCallback() {
            @Override
            public void onCheckFail() {
                Toast.makeText(mContext, "文件不存在", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResult(boolean b) {
                //返回值为true是存在
                //返回值false是不存在
                if(!b){
                    SharedPreferencesUtil.put(mContext, key, -1L);
                }else{
//                    Toast.makeText(mContext, "文件存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public  void showWelcomImg(){
        long  taskId = SharedPreferencesUtil.getLong(mContext, SunmiImgPath.WELCOME_IMG_ID);
        if(taskId != -1L){
            showImgWelcome(taskId);
            return;
        }
        File file = new File(SunmiImgPath.pathwelcom);
        if(!file.exists()){
            Toast.makeText(mContext, "欢迎图片不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        taskId=mDSKernel.sendFile(
                DSKernel.getDSDPackageName(), SunmiImgPath.pathwelcom,
                new ISendCallback() {
                    @Override
                    public void onSendSuccess(long taskId) {
                        showImgWelcome(taskId);
                        SharedPreferencesUtil.put(mContext, SunmiImgPath.WELCOME_IMG_ID, taskId);
                    }

                    @Override
                    public void onSendFail(int i, String s) {
                    }

                    @Override
                    public void onSendProcess(long totle, long sended) {
                    }
                }
        );
    }

    private void showImgWelcome(long taskId) {
//        Log.e("lh","showImgWelcome--");
        String json1 = UPacketFactory.createJson(DataModel.SHOW_IMG_WELCOME,"");
        mDSKernel.sendCMD(DSKernel.getDSDPackageName(),json1,taskId,null);
    }

    public void showPrice(String title,String content,ISendCallback callback){
        JSONObject object=new JSONObject();
        try {
            object.put("title",title);
            object.put("content",content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        DataPacket dataPacket=null;
        if(TextUtils.isEmpty(title)
                && SPManage.getInstance().getString("screenSize").equals("7")){
            dataPacket = UPacketFactory.buildShowSingleText(DSKernel.getDSDPackageName(),
                        object.toString(),callback);
        }else{
            dataPacket= UPacketFactory.buildShowText(DSKernel.getDSDPackageName(),
                    object.toString(),callback);
        }
        SunmiSdkManage.mDSKernel.sendData(dataPacket);
    }

    public void showSceerTab(List<ItemBean> list, List<KVP> KVPList,
                             ISendCallback callback){
        ListingBean listing=new ListingBean(Constant.SHOW_SCREENTAB_TITLE,getTabHead(),
                list,KVPList);
        String json = new Gson().toJson(listing);
        DataPacket dataPacket=UPacketFactory.buildPack(DSKernel.getDSDPackageName(),
                DSData.DataType.DATA,DataModel.TEXT,json,callback);
        SunmiSdkManage.mDSKernel.sendData(dataPacket);
    }

    private HeadBean getTabHead(){
        if(mHeadBean==null){
             mHeadBean=new HeadBean("序列号","商品名","单价","数量","小计");
        }
        return mHeadBean;
    }





}
